<?php
	//Check to see if there is a valid session
	$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	

	//Check to see if the user has rights to be on LivingWeb
	$PageRights = array('ADMIN','USER');
	$UserRoles = $Security->ValidateUser($SessionInfo['username'], $AppID, $PageRights, 1);

	//Grab the user info
	$UserInfo = $User->GetUserInfo($SessionInfo['username']);

	if(isset($_GET['PID']))
	{
		global $DB;
		$DB->query("SELECT * FROM pages p JOIN page_content pc ON p.PageID = pc.PageID WHERE p.PageID = '" . $_GET['PID'] . "'");
		if(!($row = $DB->fetch_row()))
		{
			print 'There was an unexpected error.';
			exit();
		}
	}

	if(isset($_POST['PID']))
	{
		global $DB;

		if(!isset($_POST['cbkAdvanced']))
		{
			$db_string = $DB->compile_db_update_string(
			array (
				'LastEditDate'    		=> date("Y-m-d H:i:s"),
				'LastEditBy'		=> $SessionInfo['username'],
				'advanced' => '0')
				);

			$DB->query("UPDATE pages SET " . $db_string . " WHERE PageID = '" . $_POST['PID'] . "'");

			//get rid of all of the undesireable tags
			$content = ereg_replace('<(/)?font[^>]*>','',$_POST['txtFCKeditor']);

			//let the DB class handle the replacement
			$content = ereg_replace("\\\'","'",$content);

			$db_string = $DB->compile_db_update_string(
			array (
				'Content'		=> $content)
						);

			$DB->query("UPDATE page_content SET " . $db_string . " WHERE PageID = '" . $_POST['PID'] . "'");
		}
		else
		{

			$db_string = $DB->compile_db_update_string(
			array (
				'LastEditDate'    		=> date("Y-m-d H:i:s"),
				'LastEditBy'		=> $SessionInfo['username'],
				'advanced' => '1')
				);

			$DB->query("UPDATE pages SET " . $db_string . " WHERE PageID = '" . $_POST['PID'] . "'");

			//get rid of all of the undesireable tags
			$content = ereg_replace('<(/)?font[^>]*>','',$_POST['txtFCKeditor']);

			//let the DB class handle the replacement
			$content = ereg_replace("\\\'","'",$content);

			$db_string = $DB->compile_db_update_string(
			array (
				'Content'		=>  $content)
						);

			$DB->query("UPDATE page_content SET " . $db_string . " WHERE PageID = '" . $_POST['PID'] . "'");
		}
		header("Location: index.php?action=edit&PID=" . $_POST['PID']);
	}

	if($row['advanced'] == 1)
	{
		$checked_string = "checked";
	}
	else
	{
		$checked_string  = "";
	}

?>
<form name="frmEditContent" action="index.php?action=edit&edit_action=edit_content" method="post">
<table cellpadding="5" cellspacing="0" style="border:1px solid #D8DA85;" width="100%">
	<tr>
		<td>
			<table cellpadding="3" cellspacing="0" width="100%" style="border-bottom:3px double #D8DA85;">
				<tr>
					<td valign="middle"><span class="TB1">Content Block</span></td>
					<td valign="middle" align="right"><table cellpadding="0" cellspacing="0"><tr><td><?php if(in_array("ADMIN",$UserRoles)){ print '<span class="T1">Advanced?</span>'; } ?></td><td valign="middle"><?php if(in_array("ADMIN",$UserRoles)){print '<input type="checkbox" name="cbkAdvanced" ' .  $checked_string . '>'; }?></td></tr></table></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<table cellpadding="0" cellspacing="2" border="0"  width="100%">
				<tr>
					<td valign="top"><?php
					include_once("CMS/fckeditor.php");
					$oFCKeditor = new FCKeditor('txtFCKeditor');
					$oFCKeditor->BasePath	= "/LivingWeb/CMS/";
					$oFCKeditor->Height		= 500;
					$oFCKeditor->Width = 700;


					if(in_array("ADMIN",$UserRoles))
					{
						$oFCKeditor->ToolbarSet	= 'Default';
					}
					else
					{
						$oFCKeditor->ToolbarSet	= 'Default';
					}

					$oFCKeditor->Value		= $row['Content'];
					$oFCKeditor->Create();
				?></td>
				</tr>
			</table>
			<?php setcookie("MID", $row['MinistryID'],false,"/"); ?>
			<input type="hidden" value="<?php print $row['PageID']; ?>" name="PID">
			<input type="hidden" value="<?php print $row['MinistryID']; ?>" name="txtMinistryID">
		</td>
	</tr>
</table>
</form>
